<template>
  <div class="index-page">
    <h3>首页大图编辑</h3>
    <div class="row" style="padding:20px;">
      <input type="button" class="btn btn-info" @click="addNewOPtion()" value="新增大图">
      <div class="row_height">
        <div class="col-md-4" v-for="(item,index) in main_list" :key="index">
          <div class="img_box">
            <img :src="item.img_file" alt="" class="full-width">
          </div>
          <div class="btn_box">
            <input type="button" value="编辑" @click="changeData(item)" class="btn btn-info">
            <input type="button" value="删除" @click="deleteData(item)" class="btn btn-danger">
          </div>
        </div>
      </div>
    </div>

    <!--新增弹窗代码-->
    <el-dialog title="新增" :visible.sync="dialog_show">
      <el-form enctype="multipart/form-data" id="uploadForm">
        <el-form-item label="选择图片" label-width="120px">
          <input type="file" name="add_pic" id="add_img">
        </el-form-item>
        <el-form-item label="跳转链接" label-width="120px">
          <el-input auto-complete="off" v-model="pic_url" name="pic_url"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_show = false">取 消</el-button>
        <el-button type="primary" @click="add_new()">确 定</el-button>
      </div>
    </el-dialog>
    <!--修改弹窗代码-->
    <el-dialog title="修改" :visible.sync="changeDialog.dialog_show">
      <el-form enctype="multipart/form-data" id="uploadForms">
        <el-form-item label="选择图片" label-width="120px">
          <input type="file" name="add_pic" id="change_img">
        </el-form-item>
        <el-form-item v-show="false" label="" label-width="120px">
          <el-input v-model="changeDialog.res_id" name="res_id"></el-input>
        </el-form-item>
        <el-form-item label="跳转链接" label-width="120px">
          <el-input auto-complete="off" v-model="changeDialog.pic_url" name="pic_url"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_show = false">取 消</el-button>
        <el-button type="primary" @click="change_func()">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import g from '../../components/global.js'

export default {
  components: {

  },
  data() {
    return {
      dialog_show: false,
      pic_url: '',
      main_list: [],
      changeDialog: {
        pic_url: '',
        dialog_show: false,
        res_id: ''
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    //新增 弹窗显示
    addNewOPtion() {
      clear()
      this.dialog_show = true
    },
    //新增 提交数据
    add_new() {
      var that = this
      if ($('#add_img').val() == '') {
        that.ts_dialog('请选择图片')
        return
      }
      var formData = new FormData($('#uploadForm')[0])
      formData.append('pic_url', that.pic_url)
      $.ajax({
        url: g.res_url + '/api/v1/upload',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data) {
            that.$message({
              message: '操作成功！',
              type: 'success'
            })
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络繁忙！')
        }
      })
      that.dialog_show = false
    },
    ts_dialog(cont) {
      this.$alert(cont, '提示', {
        confirmButtonText: '确定'
      })
    },
    //获取数据
    getData() {
      $.ajax({
        url: g.res_url + '/api/v1/getIndex',
        type: 'post',
        success: data => {
          this.main_list = JSON.parse(data)
  
        }
      })
    },
    //修改 弹窗显示
    changeData(row) {
      clear()
      this.changeDialog.dialog_show = true
      this.changeDialog.pic_url = row.img_url
      this.changeDialog.res_id = row.id
    },
    //修改提交数据
    change_func() {
      var that = this
      var formData = new FormData()
      if ($('#change_img').val() == '') {
      } else {
        formData.append('add_pic', $('#change_img')[0].files[0])
      }
      formData.append('pic_url', that.changeDialog.pic_url)
      formData.append('res_id', that.changeDialog.res_id)
      $.ajax({
        url: g.res_url + '/api/v1/changeData',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data == 'success') {
            that.$message.success('操作成功！')
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络错误！')
        }
      })
      that.changeDialog.dialog_show = false
    },
    deleteData(row) {
      var that = this
      $.ajax({
        url: g.res_url + '/api/v1/index_del',
        type: 'post',
        data: {
          del_id: row.id
        },
        success: function(data) {
          if (data == 'success') {
            that.$message.success('操作成功！')
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(err) {
          that.$message.error('网络错误！')
        }
      })
    }
  }
}
function clear() {
  $('#change_img').val('')
  $('#add_img').val('')
}
</script>
<style scoped>
.row_height {
  height: 150px !important;
  margin: 28px 0;
}
.btn_box {
  margin-left: 45px;
}
.img_box {
  width: 200px;
  height: 200px;
  overflow: hidden;
  margin-bottom: 20px;
}
.full-width {
  width: 100%;
}
.col-md-4 {
  margin-top: 30px;
}
</style>